﻿#pragma once

#define USE 0

#include <QtWidgets/QMainWindow>
#include <QMessageBox>
#include <QString>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QElapsedTimer>
#include <QDebug>
#include <QSemaphore>
#include <QThread>

#include <opencv.hpp>
//#include <opencv2/wechat_qrcode.hpp>

#include "include/dh/dhconfigsdk.h"
#include "include/dh/dhnetsdk.h"
#include "include/libdmtx/dmtx.h"
#include "include/yolo/YOLO11.h"

#include "Camera.h"
#include "SignalStation.h"

typedef struct {
	int idx;
	cv::Mat image;
}MatImage;

typedef struct {
	int idx;
	std::vector<int> dms;
}Dm;

class Capture : public QObject
{
	Q_OBJECT

public:
	Capture(int id, QWidget* parent = nullptr);
	~Capture();

	void identify(cv::Mat mat); //识别模式
	int detec_dm(cv::Mat mat); //检测dm码
	void calibrate(cv::Mat src); //校正相机
	void setup_camera(Camera* camera);

	static QPixmap pix_from_mt(cv::Mat mt);
	
	void set_rec(cv::Rect rect);
	void set_identify(bool flag); //校准，识别？

	bool get_identify() { return is_identify; }

	static Capture* s_this;

	//相机资源
	//bool _busy;
	//QSemaphore* _semaphore;

	cv::Rect _rec;

	bool _debug = false;

private:
	
	void inference_dm(cv::Mat mat);
	std::vector<cv::Rect> locate(cv::Mat src); //定位
	void qr_locate(cv::Mat src); //qr定位
	void BrightnessAndContrastAuto(const cv::Mat& src, cv::Mat& dst, float clipHistPercent = 0);
	void read_camera_param();

	QWidget* parent;

	//debug
	QElapsedTimer _counter;

	bool is_calibrate; //校准
	bool is_identify; //识别

	//图像校正
	cv::Mat _mtx;
	cv::Mat _dist;

	int _factor = 1;

	int camera_id = 0;

	Camera* g_camera = nullptr;


signals:
	void new_picture(MatImage mat);
	void matrix_data(Dm dm);

private slots:
	void new__picture(cv::Mat src);
};

